export default {
  namespaced: true,
  state: {
    token: uni.getStorageSync('token'),
    userInfo: uni.getStorageSync('userInfo') || {}
  },
  mutations: {
    // 设置token
    setToken(state, data) {
      state.token = data
      uni.setStorageSync('token', data)
    },
    // 设置用户信息
    setUserInfo(state, data) {
      state.userInfo = data
      uni.setStorageSync('userInfo', data)
    }
  },
  actions: {
    wxLoginAction(context) {
      // 调用 uni.login 获取登录凭证code，提交给服务器端
      uni.login({
        success: async (res) => {
          // 发起登录请求
          let result = await uni.$http.post('/auth/loginByWeixin', {
            code: res.code
          })
          console.log(result);
          // 进行存储
          context.commit('setToken', result.token)
          // 发起获取用户资料请求
          let result2 = await uni.$http.get('/user/profile')
          context.commit('setUserInfo', result2)
          console.log(result2);
        }
      })
    }
  }
}
